package ext.acae2.mail;

import java.util.Properties;
import java.util.Vector;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @desription email file
 * @author cnie
 * @date 2013-3-6 下午3:21:12
 */
public class MailFile {

	public InternetAddress from = null;

	public Vector to = new Vector();

	public String subject = "";

	public String text = "";

	public String mailHost = "";

	public Properties props = new Properties();

	public void send() throws MessagingException {

		Session session = Session.getDefaultInstance(props);
		MimeMessage message = new MimeMessage(session);

		Transport bus = session.getTransport("smtp");
		bus.connect();
		message.setFrom(from);
		if (to.size() > 0) {
			for (int i = 0; i < to.size(); i++) {
				InternetAddress iac = new InternetAddress(
						(String) to.elementAt(i));
				message.addRecipient(Message.RecipientType.TO, iac);
			}
		}
		message.setSubject(subject);
		message.setContent(text,"text/html;charset=UTF-8");// text/plain
		bus.sendMessage(message, message.getAllRecipients());
		bus.close();

	}

	public void setFrom(String _from) {
		try {
			from = new InternetAddress(_from);
		} catch (AddressException e) {
			e.printStackTrace();
		}
	}

	public void setTo(String _to) {
		to.addElement(_to);
	}

	public void setTo(Vector _to) {
		to = _to;
	}

	public void setSubject(String _subject) {
		subject = _subject;
	}

	public void setText(String _text) {
		text = _text;
	}

	public void setMailHost(String _host) {
		mailHost = _host;
		props.put("mail.smtp.host", mailHost);
		props.put("mail.smtp.auth", "false");
	}

}
